package com.cyf.db.mapper;

import com.cyf.db.bean.ParkingLockInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 车锁信息
 * Created by cage on 2017/4/7.
 */

public interface ParkingLockInfoMapper {

    @Insert("insert parkinglock_info(name, macAddress, status, shareStatus, lockStatus, appUserId, mobile, createdTime, bookingStatus) " +
            "values(#{lockInfo.name}, #{lockInfo.macAddress}, 1, 0, #{lockInfo.lockStatus}, #{lockInfo.appUserId}, #{lockInfo.mobile}, #{lockInfo.createdTime}, 0)")
    void insertLock(@Param("lockInfo") ParkingLockInfo lockInfo);

    // 获取可以使用的地锁
    @Select("select * from parkinglock_info where lockStatus = 0 and status = 1 and parkingId = #{parkingId}")
    List<ParkingLockInfo> getUnLockInfos(@Param("parkingId") Integer parkingId);

    @Select("select * from parkinglock_info where id = #{id}")
    ParkingLockInfo getById(@Param("id") Integer id);

    // 查询车场的总预约车位数
    @Select("select count(id) from parkinglock_info where parkingId = #{parkingId} and status = 1 and bookingStatus = 0")
    Integer getCountByParkingId(@Param("parkingId") Integer parkingId);

    @Select("select * from parkinglock_info where parkingId = #{parkingId} and status = 1 and bookingStatus = 0 limit 0, 1")
    ParkingLockInfo getUsedByParkingId(@Param("parkingId") Integer parkingId);

    @Select("select * from parkinglock_info where appUserId = #{appUserId}")
    List<ParkingLockInfo> getByAppUserId(@Param("appUserId") Integer appUserId);

    // 根据appUserId和地锁id查找地锁
    @Select("select * from parkinglock_info where appUserId = #{appUserId} and id = #{id}")
    ParkingLockInfo getByAppUserIdAndId(@Param("appUserId") Integer appUserId, @Param("id") Integer id);

    @Select("select * from parkinglock_info where macAddress = #{macAddress}")
    ParkingLockInfo getByMacAddress(@Param("macAddress") String macAddress);

    // 更新锁信息（名称、mac）
    @Update("update parkinglock_info set name = #{lockInfo.name}, macAddress = #{lockInfo.macAddress} where id = #{lockInfo.id}")
    void updateLock(@Param("lockInfo") ParkingLockInfo lockInfo);

    // 更新锁定状态
    @Update("update parkinglock_info set lockStatus = #{lockStatus} where id = #{id}")
    void updateLockStatus(@Param("id") Integer id, @Param("lockStatus") int lockStatus);

    // 更新共享状态
    @Update("update parkinglock_info set shareStatus = #{shareStatus} where id = #{id}")
    void updateShareStatus(@Param("shareStatus") Integer shareStatus, @Param("id") Integer id);

    // 删除
    @Delete("delete from parkinglock_info where id = #{id}")
    void deleteById(@Param("id") Integer id);

    // 取消预约，更新锁的 预约状态，锁定状态
    @Update("update parkinglock_info set bookingStatus = 0 , lockStatus = 0 where id = #{id}")
    void cacelBooking(@Param("id") Integer id);

    // 预约，更新锁的 预约状态
    @Update("update parkinglock_info set bookingStatus = 1 where id = #{id}")
    void booking(@Param("id") Integer id);
}
